//
//  ViewController.m
//  12-多个TextFiled
//
//  Created by wangneihan on 15/12/24.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "diyKeyBoardBar.h"

@interface ViewController () <diyKeyBoardBarDelegate>

@property(nonatomic,weak)diyKeyBoardBar *toolBar;

@property (weak, nonatomic) IBOutlet UIView *textView;

@property(nonatomic,strong)NSArray *textFiles;

@end

@implementation ViewController

-(diyKeyBoardBar *)toolBar
{
    if(_toolBar == nil)
    {
        _toolBar = [diyKeyBoardBar diykeyboardbar];
        _toolBar.delegate = self;
    }
    return  _toolBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
#warning 我们在使用UITextView和UITextField的时候，可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图，通常是UIToolBar，用于回收键盘。
    // 1. 订阅键盘事件  用于显示/隐藏toolBar
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHide) name:UIKeyboardWillHideNotification object:nil];
    NSMutableArray *filds = [NSMutableArray array];
    // 2. 遍历textFiled 设置UITextField inputAccessoryView
    for(UIView *subView in self.textView.subviews)
    {
        if([subView isKindOfClass:[UITextField class]])
        {
            UITextField *tf = (UITextField *)subView;
            tf.inputAccessoryView = self.toolBar;
            
            tf.tag = filds.count;
            [filds addObject:tf];
        }
    }
    self.textFiles = filds;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)diyKeyBoardBarClick:(diyKeyBoardBar *)diykeyboardbar btndidSelected:(UIBarButtonItem *)item
{
    switch (item.tag) {
        case 0:
            //上一个
            [self preTextFiled];
            break;
        case 1:
            //下一个
            [self nextTextFiled];
            break;
        case 2:
            [self.view endEditing:YES];
            break;
        default:
            break;
    }
}

-(void)willShow:(NSNotification *)noti
{
    NSLog(@"%@",noti);
//    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 315}}";
//    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 421}, {414, 315}}";
    // 1. 判断 上、下 一个是否可用
    UITextField *textFiled = self.textFiles[[self indexOfFirstResponder]];
    if(textFiled.tag == 0)
            self.toolBar.preBtn.enabled = NO;
    else
            self.toolBar.preBtn.enabled = YES;
    if(textFiled.tag == self.textFiles.count-1)
            self.toolBar.nextBtn.enabled = NO;
    else
            self.toolBar.nextBtn.enabled = YES;
    
    //currentTf.frame.origin.y +  currentTf.frame.size.height
    CGFloat maxY = CGRectGetMaxY(textFiled.frame) + self.textView.frame.origin.y;
    
    
    //2.获取键盘的y值
    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbY = kbEndFrm.origin.y;
    
    //3.进行比较
    //计算距离
    CGFloat delta = kbY - maxY;
    NSLog(@"delta : %f ",delta);
    
    // 4. 如果距离小于0  说明没挡住。。。
    if(delta < 0){//需要往上移
        //添加个动画
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, delta);
        }];
        
    }

    
    
}

-(void)willHide
{
    
}

-(void)preTextFiled
{
#warning [textfield becomeFirstResponder],取消焦点的方法 resignFirstResponder
    NSInteger index = [self indexOfFirstResponder];
    self.toolBar.nextBtn.enabled = YES;
    
    //1. 取消原控件焦点
    [self.textFiles[index] resignFirstResponder];
    //2. 设置新的控件获取焦点
    UITextField *textFiled = self.textFiles[index - 1];
    [textFiled becomeFirstResponder];
}

-(void)nextTextFiled
{
#warning [textfield becomeFirstResponder],取消焦点的方法 resignFirstResponder
    NSInteger index = [self indexOfFirstResponder];
    self.toolBar.preBtn.enabled = YES;
    
    //1. 取消原控件焦点
    [self.textFiles[index] resignFirstResponder];
    //2. 设置新的控件获取焦点
    UITextField *textFiled = self.textFiles[index + 1];
    [textFiled becomeFirstResponder];
}

-(NSInteger)indexOfFirstResponder
{
    for (UITextField *tf in self.textFiles) {
        if(tf.isFirstResponder)
        {
            return tf.tag;
        }
    }
    return  -1;
}



@end
